using Microsoft.EntityFrameworkCore;
using ProduceBasicData.Api.DTO;
using ProduceBasicData.Domain.CodeGeneration;
using ProduceBasicData.Infrastructure;
using ProduceBasicData.Infrastructure.Interface;
using System.Reflection;

namespace ProduceBasicData.Api.Services
{
    /// <summary>
    /// 编码规则管理服务实现
    /// </summary>
    public class CodeRuleService : ICodeRuleService
    {
        private readonly IBaseRespository<CodeRule> _codeRuleRepository;
        private readonly ICodeGeneratorService _codeGeneratorService;
        private readonly MyDBContext _dbContext;
        private readonly Assembly _domain;

        public CodeRuleService(
            IBaseRespository<CodeRule> codeRuleRepository,
            ICodeGeneratorService codeGeneratorService,
            MyDBContext dbContext)
        {
            _codeRuleRepository = codeRuleRepository;
            _codeGeneratorService = codeGeneratorService;
            _dbContext = dbContext;
            _domain = typeof(ProduceBasicData.Domain.Entities.BaseEntity).Assembly;
        }

        /// <summary>
        /// 获取指定实体的编码规则
        /// </summary>
        public async Task<CodeRule?> GetRuleByEntityAsync(string classFullName)
        {
            return await _codeRuleRepository.GetAll()
                .FirstOrDefaultAsync(x => x.ClassFullName == classFullName && x.IsEnabled && !x.Isdel);
        }

        /// <summary>
        /// 保存编码规则
        /// </summary>
        public async Task<bool> SaveRuleAsync(CodeRule rule)
        {
            try
            {
                await _codeRuleRepository.AddAsync(rule);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 更新编码规则
        /// </summary>
        public async Task<bool> UpdateRuleAsync(CodeRule rule)
        {
            try
            {
                await _codeRuleRepository.Update(rule);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 删除编码规则
        /// </summary>
        public async Task<bool> DeleteRuleAsync(int id)
        {
            try
            {
                var rule = await _codeRuleRepository.GetModelAsync(id);
                if (rule != null)
                {
                    rule.Isdel = true;
                    await _codeRuleRepository.Update(rule);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 获取所有编码规则
        /// </summary>
        public async Task<List<CodeRule>> GetAllRulesAsync()
        {
            return await _codeRuleRepository.GetAll()
                .Where(x => !x.Isdel)
                .ToListAsync();
        }

        /// <summary>
        /// 根据规则自动生成编码
        /// </summary>
        public async Task<string?> AutoGenerateCodeAsync(string classFullName)
        {
            var rule = await GetRuleByEntityAsync(classFullName);
            if (rule == null)
            {
                return null;
            }

            var generateRequest = new GenerateCodeRequest
            {
                ClassFullName = rule.ClassFullName,
                CodeFieldName = rule.CodeFieldName,
                RuleDateType = (RuleDateType)rule.RuleDateType,
                GrowthType = (GrowthType)rule.GrowthType,
                DateFieldName = rule.DateFieldName,
                Prefix = rule.Prefix,
                Digits = rule.Digits
            };

            return await _codeGeneratorService.GenerateAsync(generateRequest);
        }
    }
}
